package com.ht.medicaltreatment.activity

import android.content.Context
import android.graphics.Color
import android.view.Gravity
import androidx.activity.viewModels
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.PathUtils
import com.blankj.utilcode.util.ToastUtils
import com.gym.baselib.base.BaseActivity
import com.gym.baselib.utils.CacheUtils
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.base.CacheKey
import com.ht.medicaltreatment.utils.SoundUtils
import com.ht.medicaltreatment.viewmodel.LoginViewModel
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshFooter
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.DefaultRefreshFooterCreator
import com.scwang.smart.refresh.layout.listener.DefaultRefreshHeaderCreator

class SplashActivity : BaseActivity() {

    private val mViewModel by viewModels<LoginViewModel>()

    override fun getLayoutId() = R.layout.activity_splash_layout

    override fun init() {
        ////StatusBarCompat.setTranslucent(window,true)
        //先加载下声音
        SoundUtils.load()
        initRefresh()
        initSDK()

        val token = CacheUtils.getStringCache(CacheKey.TOKEN)

        startActivity(LoginActivity::class.java)
        finish()

        /*if(token == ""){
            startActivity(LoginActivity::class.java)
            finish()
        }else{
            EasyConfig.getInstance().addHeader("Mobile-Custom-Token",token)
            startActivity(MainActivity::class.java)
            finish()
        }*/
    }

    private fun initSDK() {

        val file = PathUtils.getExternalStoragePath() + "/Android/日志文件"
        val orExistsDir = FileUtils.createOrExistsDir(file)
        if (orExistsDir) {
            LogUtils.getConfig().setDir(file)
        }

        ToastUtils.getDefaultMaker()
            .setBgColor(Color.BLACK)
            .setTextColor(Color.WHITE)
            .setGravity(Gravity.CENTER,0,0)

        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(object : DefaultRefreshHeaderCreator{
            override fun createRefreshHeader(
                context: Context,
                layout: RefreshLayout
            ): RefreshHeader {
                layout.setEnableFooterFollowWhenNoMoreData(true)
                layout.setPrimaryColorsId(R.color.white,R.color.line)
                val classHeader = ClassicsHeader(this@SplashActivity)
                classHeader.setPrimaryColor(getColor(R.color.white))
                layout.setEnableRefresh(false)
                layout.setEnableLoadMore(true)
                return classHeader
            }
        });

        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(object : DefaultRefreshFooterCreator {
            override fun createRefreshFooter(
                context: Context,
                layout: RefreshLayout
            ): RefreshFooter {
                layout.setEnableFooterFollowWhenNoMoreData(true);
                layout.setEnableAutoLoadMore(true);
                layout.setEnableRefresh(false)
                layout.setEnableLoadMore(true)
                val footer = ClassicsFooter(context)
                footer.setHorizontalGravity(Gravity.CENTER_HORIZONTAL)
                return footer
            }
        });
    }

    private fun initRefresh() {
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            layout.setPrimaryColors(Color.RED)
            val classHeaderCreator = ClassicsHeader(context)
            classHeaderCreator.setPrimaryColor(getColor(R.color.blueDeep))
            classHeaderCreator.setAccentColorId(R.color.white)
            classHeaderCreator
        }

        SmartRefreshLayout.setDefaultRefreshFooterCreator(object : DefaultRefreshFooterCreator {
            override fun createRefreshFooter(
                context: Context,
                layout: RefreshLayout
            ): RefreshFooter {
                layout.setPrimaryColorsId(R.color.black)
                layout.setEnableAutoLoadMore(false)
                layout.setEnableLoadMore(false)
                return ClassicsFooter(context)
            }
        })
    }
}